package com.logic.landseaserver.models.printlog.ws;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.util.LoggerHelper;
import com.logic.landseaserver.models.printlog.bean.PrintLogReq;
import com.logic.landseaserver.models.printlog.service.PrintLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;

/**
 * Created by Junny on 2018/7/17.
 */
@Path("/printLog")
@Api(value = "打印记录")
public class PrintLogRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(PrintLogRest.class);

    @Autowired
    private PrintLogService printLogService;

    @POST
    @Path("/savePrintLog")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "保存打印记录")
    public Response savePrintLog(@ApiParam(value = "param", required = false) PrintLogReq req)
    {
        CommonResultMap map = new CommonResultMap();
        try{
            printLogService.savePrintLog(req);
            map.setResult(CommonResult.SUCCESS);
        } catch (LandseaException e) {
            LoggerHelper.handleException(LOGGER, "保存打印记录失败", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }

    @POST
    @Path("/queryCount")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "获取打印次数")
    public Response queryCount(@ApiParam(value = "param", required = false) PrintLogReq req)
    {
        CommonResultMap map = new CommonResultMap();
        try{
            HashMap mp = new HashMap();
            mp.put("count",printLogService.queryCount(req));
            map.setData(mp);
            map.setResult(CommonResult.SUCCESS);
        }catch (LandseaException e){
            LoggerHelper.handleException(LOGGER, "获取打印次数失败", e, map);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
}
